package cube.net.socket;

import cube.data.FiFo;
import cube.utils.exec.BlockExecutor;

/**
 * Socket客户端
 * 
 * @author abiao
 */
public class SocketClient extends SocketConnectImpl implements SocketConnect {

    /**
     * 数据接收存储FiFo
     */
    private FiFo<byte[]> fifo = new FiFo<byte[]>();

    /**
     * Socket客户端
     */
    public SocketClient(String ip, int port) {
        super(ip, port);
    }

    /**
     * 接收数据存入FiFo
     */
    public void recv(byte[] data) {
        fifo.put(data);
    }

    /**
     * 阻塞超时读取数据
     */
    public byte[] blockRead(int timeout) {
        return new BlockExecutor<byte[]>() {
            public byte[] action() {
                return fifo.top();
            }

            public boolean condition() {
                return !fifo.empty();
            }
        }.blockExecute(timeout);
    }
}
